<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客注册页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="css/components.css">

</head>

<body>
<div class="nav">
    <div class="logo-container">
        <div class="logo-icon">B</div>
        <span class="blog-title">我的博客系统</span>
    </div>
    <div class="space"></div>
    <a class="nav-span" href="blog_list.html">主页</a>
    <a class="nav-span" href="blog_edit.html">写博客</a>
</div>

<div class="container-login">
    <div class="login-dialog" style="height: 420px;">
        <h3>用户注册</h3>
        <div class="row">
            <span>用户名</span>
            <input type="text" name="username" id="username" placeholder="3-20个字符" autocomplete="username" maxlength="20">
        </div>
        <div class="row">
            <span>密码</span>
            <input type="password" name="password" id="password" placeholder="至少6位密码" autocomplete="new-password" maxlength="50">
        </div>
        <div class="row">
            <span>确认密码</span>
            <input type="password" name="confirmPassword" id="confirmPassword" placeholder="再次输入密码" autocomplete="new-password" maxlength="50">
        </div>
        <div class="row">
            <span>GitHub链接</span>
            <input type="url" name="githubUrl" id="githubUrl" placeholder="可选，GitHub个人页面链接" maxlength="200">
        </div>
        <div class="row">
            <button id="submit" onclick="register()">注册</button>
        </div>
        <div class="row">
            <span style="font-size: 14px;">已有账号？<a href="blog_login.html" style="color: #007bff; text-decoration: none;">立即登录</a></span>
        </div>
    </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    // jQuery 加载失败的备用方案
    if (typeof jQuery === 'undefined') {
        document.write('<script src="blog-editormd/lib/jquery.min.js"><\/script>');
    }
</script>
<script src="js/common.js"></script>
<script>
    function register() {
        // 获取表单数据
        const username = $("#username").val().trim();
        const password = $("#password").val();
        const confirmPassword = $("#confirmPassword").val();
        const githubUrl = $("#githubUrl").val().trim();
        
        // 前端验证
        if (!username) {
            showMessage("请输入用户名", "warning");
            $("#username").focus();
            return;
        }
        
        // 用户名格式验证
        if (!/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(username)) {
            showMessage("用户名只能包含字母、数字、下划线和中文", "warning");
            $("#username").focus();
            return;
        }
        
        if (username.length < 3 || username.length > 20) {
            showMessage("用户名长度必须在3-20字符之间", "warning");
            $("#username").focus();
            return;
        }
        
        if (!password) {
            showMessage("请输入密码", "warning");
            $("#password").focus();
            return;
        }
        
        if (password.length < 6) {
            showMessage("密码长度不能少于6位", "warning");
            $("#password").focus();
            return;
        }
        
        if (password.length > 50) {
            showMessage("密码长度不能超过50位", "warning");
            $("#password").focus();
            return;
        }
        
        if (!confirmPassword) {
            showMessage("请确认密码", "warning");
            $("#confirmPassword").focus();
            return;
        }
        
        if (password !== confirmPassword) {
            showMessage("两次输入的密码不一致", "warning");
            $("#confirmPassword").focus();
            return;
        }
        
        // GitHub URL 验证（可选）
        if (githubUrl && !/^https?:\/\/.+/.test(githubUrl)) {
            showMessage("GitHub链接格式不正确，请以http://或https://开头", "warning");
            $("#githubUrl").focus();
            return;
        }
        
        // 显示加载状态
        showLoading("#submit", "注册中...");
        
        // 发送注册请求
        $.ajax({
            type: "post",
            url: "/user/register",
            contentType: "application/json",
            data: JSON.stringify({
                userName: username,
                password: password,
                confirmPassword: confirmPassword,
                githubUrl: githubUrl || null
            }),
            success: function(result) {
                hideLoading("#submit");
                if (result != null && result.code == "SUCCESS") {
                    showMessage("注册成功！即将跳转到登录页面", "success");
                    setTimeout(() => {
                        location.href = "blog_login.html";
                    }, 2000);
                } else {
                    showMessage(result.errMsg || "注册失败，请重试", "error");
                }
            },
            error: function(xhr, status, error) {
                hideLoading("#submit");
                handleAjaxError(xhr, "注册失败，请重试");
            }
        });
    }
    
    // 页面加载完成后的处理
    $(document).ready(function() {
        // 检查是否已经登录
        const token = localStorage.getItem('userToken');
        if (token) {
            showMessage('您已经登录，正在跳转...', 'info');
            setTimeout(() => {
                location.href = 'blog_list.html';
            }, 1000);
            return;
        }
        
        // 聚焦到用户名输入框
        $('#username').focus();
        
        // 实时验证
        $('#username').on('input', function() {
            const username = $(this).val().trim();
            if (username && (username.length < 3 || username.length > 20)) {
                $(this).css('border-color', '#f44336');
            } else if (username && !/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(username)) {
                $(this).css('border-color', '#f44336');
            } else {
                $(this).css('border-color', '#4CAF50');
            }
        });
        
        $('#password').on('input', function() {
            const password = $(this).val();
            if (password && password.length < 6) {
                $(this).css('border-color', '#f44336');
            } else if (password) {
                $(this).css('border-color', '#4CAF50');
            }
        });
        
        $('#confirmPassword').on('input', function() {
            const password = $('#password').val();
            const confirmPassword = $(this).val();
            if (confirmPassword && password !== confirmPassword) {
                $(this).css('border-color', '#f44336');
            } else if (confirmPassword) {
                $(this).css('border-color', '#4CAF50');
            }
        });
    });
    
    // 回车键提交
    $(document).keypress(function(e) {
        if (e.which == 13) {
            register();
        }
    });
</script>
</body>

</html>